package com.wteam.dragon.shop.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.shop.mapper.BrandHomeMapper;
import com.wteam.dragon.shop.mapper.BrandMapper;
import com.wteam.dragon.shop.mapper.ProductMapper;
import com.wteam.dragon.shop.pojo.bean.Brand;
import com.wteam.dragon.shop.pojo.bean.BrandHome;
import com.wteam.dragon.shop.pojo.bean.Product;
import com.wteam.dragon.shop.service.BrandService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2021/3/23 17:06
 */
@Service
@RequiredArgsConstructor
public class BrandServiceImpl implements BrandService {
    private final BrandMapper brandMapper;
    private final ProductMapper productMapper;
    private final BrandHomeMapper brandHomeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addBrand(Brand brand) {
        //如果品牌首字母为空 则将英文名字的第一个字母为品牌首字母
        if (brand.getFirstLetter() == null) {
            brand.setFirstLetter(brand.getNameEn().substring(0, 1));
        }
        brandMapper.insert(brand);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delBrand(List<Long> ids) {
        brandMapper.deleteBatchIds(ids);
        // 删除推荐商品

        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editBrand(Brand brand) {
        //如果品牌首字母为空 则将英文名字的第一个字母为品牌首字母
        if (brand.getFirstLetter() == null) {
            brand.setFirstLetter(brand.getNameEn().substring(0, 1));
        }
        //如果名称进行了更改 编辑商品中的品牌名称和编辑品牌推荐中的名称
        Brand old = brandMapper.selectById(brand.getId());
        if(!old.getNameEn().equals(brand.getNameEn()) || !old.getName().equals(brand.getName())){
            Product product = new Product();
            product.setBrandName(brand.getName());
            product.setBrandNameEn(brand.getNameEn());
            QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(Product::getBrandId, brand.getId());
            productMapper.update(product, queryWrapper);
            BrandHome brandHome = new BrandHome();
            brandHome.setBrandName(brand.getName());
            brandHome.setBrandNameEn(brand.getNameEn());
            QueryWrapper<BrandHome> brandHomeQueryWrapper = new QueryWrapper<>();
            brandHomeQueryWrapper.lambda()
                    .eq(BrandHome::getBrandId, brand.getId());
            brandHomeMapper.update(brandHome, brandHomeQueryWrapper);
        }
        brandMapper.updateById(brand);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

    @Override
    public ResultMessage pageBrand(Page<Brand> page, String name, Boolean showStatus) {
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .and(nameQ -> nameQ
                        .like(name != null, Brand::getName, name)
                        .or()
                        .like(name != null, Brand::getNameEn, name))
                .eq(showStatus != null, Brand::getShowStatus, showStatus)
                .orderByAsc(Brand::getSort);
        Page<Brand> brandPage = brandMapper.selectPage(page, queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("brandPage", brandPage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage getBrand(Long id) {
        Brand brand = brandMapper.selectById(id);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("brand", brand);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage editShowStatus(@RequestParam List<Long> ids, Boolean showStatus) {
        ids.forEach(id -> {
            Brand brand = new Brand();
            brand.setId(id);
            brand.setShowStatus(showStatus);
            brandMapper.updateById(brand);
        });
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

    @Override
    public ResultMessage editFactoryStatus(@RequestParam List<Long> ids, Boolean factoryStatus) {
        ids.forEach(id -> {
            Brand brand = new Brand();
            brand.setId(id);
            brand.setFactoryStatus(factoryStatus);
            brandMapper.updateById(brand);
        });
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

    @Override
    public ResultMessage getBrandName() {
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(Brand::getId, Brand::getName)
                .orderByAsc(Brand::getSort);
        List<Brand> brandList = brandMapper.selectList(queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("brandList", brandList);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }
}
